package com.snail.readyvr.util;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import com.snail.readyvr.R;
import com.snail.readyvr.interfaces.OnClickOneEdit;
import com.snail.readyvr.ui.ClearEditText;
import com.snail.readyvr.ui.CommonTopBar;

/**
 * Created by MRKING on 2016/9/29.
 */

public class DialogUtils {

    private DialogUtils(){}

    public static Dialog showNewAccount(Activity activity, String content){
        View view = activity.getLayoutInflater().inflate(R.layout.new_account_dialog_layout,null);
        TextView contentText = (TextView) view.findViewById(R.id.new_account_content);
        contentText.setText(content);
        AlertDialog.Builder builder =  new AlertDialog.Builder(activity);
        builder.setCancelable(false);
        final AlertDialog dialog =  builder
                .setView(view)
                .show();
        dialog.getWindow().setBackgroundDrawableResource(R.color.color_transparent);
        dialog.getWindow().setWindowAnimations(R.style.dialogWindowAnim);
        return dialog;
    }

    public static void showEmialTip(Activity activity, String title, String content, String email, String okString, boolean showCancel, final View.OnClickListener onClickListener){
        View view = activity.getLayoutInflater().inflate(R.layout.register_email_dialog_layout,null);


        AlertDialog.Builder builder =  new AlertDialog.Builder(activity);
        builder.setCancelable(true);
        final AlertDialog dialog =  builder
                .setView(view)
                .show();
        dialog.getWindow().setBackgroundDrawableResource(R.color.color_transparent);
        dialog.getWindow().setWindowAnimations(R.style.dialogWindowAnim);

        TextView titleText = (TextView) view.findViewById(R.id.dialog_title);
        titleText.setText(title);
        TextView contentText = (TextView) view.findViewById(R.id.dialog_content);
        if(email != null){
            int start = content.indexOf(email) - 1;
            int end = start + email.length() + 2;
            Spannable span = new SpannableString(content);
            span.setSpan(new AbsoluteSizeSpan(14,true), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            span.setSpan(new ForegroundColorSpan(Color.WHITE), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            span.setSpan(new BackgroundColorSpan(activity.getResources().getColor(R.color.new_account_email_bg_color)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            contentText.setText(span);
        }else {
            contentText.setText(content);
        }
        TextView cancelText = (TextView) view.findViewById(R.id.dialog_cancel);
        cancelText.setVisibility(showCancel?View.VISIBLE:View.GONE);
        cancelText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView okText = (TextView) view.findViewById(R.id.dialog_ok);
        okText.setText(okString);
            okText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    if(onClickListener != null)
                        onClickListener.onClick(v);

                }
            });

    }

    public static void showDownloadDialog(Activity activity, String title, String content, String cancelStr, String okStr, final View.OnClickListener onClickListener){
        View view = activity.getLayoutInflater().inflate(R.layout.download_dialog_layout,null);


        AlertDialog.Builder builder =  new AlertDialog.Builder(activity);
        builder.setCancelable(true);
        final AlertDialog dialog =  builder
                .setView(view)
                .show();
        dialog.getWindow().setBackgroundDrawableResource(R.color.color_transparent);
        dialog.getWindow().setWindowAnimations(R.style.dialogWindowAnim);

        TextView titleText = (TextView) view.findViewById(R.id.dialog_title);
        titleText.setText(title);
        TextView contentText = (TextView) view.findViewById(R.id.dialog_content);
        contentText.setText(content);

        TextView cancelText = (TextView) view.findViewById(R.id.dialog_cancel);
       if(cancelStr != null){
           cancelText.setText(cancelStr);
       }
        cancelText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView okText = (TextView) view.findViewById(R.id.dialog_ok);
        okText.setText(okStr);
        okText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if(onClickListener != null)
                    onClickListener.onClick(v);

            }
        });
    }

    public static void showOneEditDialog(Activity activity, String title, String oneStr, String cancelStr, String okStr, final OnClickOneEdit onClickOneEdit){
        View view = activity.getLayoutInflater().inflate(R.layout.change_nickname_dialog_layout,null);


        AlertDialog.Builder builder =  new AlertDialog.Builder(activity);
        builder.setCancelable(true);
        final AlertDialog dialog =  builder
                .setView(view)
                .show();
        dialog.getWindow().setBackgroundDrawableResource(R.color.color_transparent);
        dialog.getWindow().setWindowAnimations(R.style.dialogWindowAnim);
        CommonTopBar commonTopBar = (CommonTopBar) view.findViewById(R.id.dialog_top_bar);
        commonTopBar.midTextView.setText(title);commonTopBar.setOnClickCommonTopBar(new CommonTopBar.OnClickCommonTopBar() {
            @Override
            public void onClickLeftView() {
                dialog.dismiss();
            }

            @Override
            public void onClickRightView() {

            }
        });

       final ClearEditText clearEditText = (ClearEditText) view.findViewById(R.id.dialog_edit_text);
        clearEditText.setText(oneStr);

        TextView cancelText = (TextView) view.findViewById(R.id.dialog_cancel);
        if(cancelStr != null){
            cancelText.setText(cancelStr);
        }
        cancelText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView okText = (TextView) view.findViewById(R.id.dialog_ok);
        okText.setText(okStr);
        okText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //dialog.dismiss();
                if(onClickOneEdit != null)
                    onClickOneEdit.onClick(dialog, clearEditText.getText().toString());

            }
        });
    }

    public static Dialog showCheckUpdate(Activity activity, String content){
        View view = activity.getLayoutInflater().inflate(R.layout.check_update_dialog_layout,null);
        TextView contentText = (TextView) view.findViewById(R.id.check_update_tip);
        contentText.setText(content);
        AlertDialog.Builder builder =  new AlertDialog.Builder(activity);
        builder.setCancelable(false);
        final AlertDialog dialog =  builder
                .setView(view)
                .show();
        dialog.getWindow().setBackgroundDrawableResource(R.color.color_transparent);
        dialog.getWindow().setWindowAnimations(R.style.dialogWindowAnim);
        return dialog;
    }
}
